package persistance.dal;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import ucc.objects.CaisseDuJourDTO;
import ucc.objects.OperationCaisseDTO;
import ucc.request.UccViewer;
import biz.BizFactory;

public class OperationCaisseFinderImpl implements OperationCaisseFinder {

	private PreparedStatement q1;
	private PreparedStatement q2;
	private final String Q1 = "select * from caisse.operations_caisse where id_operation_caisse=?";
	private final String Q2 = "select * from caisse.operations_caisse where id_caisse_du_jour=? order by horodatage asc";

	public OperationCaisseFinderImpl() {
		this.q1 = DalBackEndServices.INSTANCE.prepare(Q1);
		this.q2 = DalBackEndServices.INSTANCE.prepare(Q2);
	}

	public OperationCaisseDTO lOperationCaisseNum(Long id) {
		ResultSet rs = null;
		try {
			q1.setObject(1, id);
			rs = q1.executeQuery();
			OperationCaisseDTO operationCaisse = BizFactory.INSTANCE
					.createEmptyOperationCaisseDTO();
			while (rs.next()) {
				operationCaisse.setId(id);
				Calendar horodatage = Calendar.getInstance();
				horodatage.setTimeInMillis(rs.getTimestamp("horodatage")
						.getTime());
				operationCaisse.setHorodatage(horodatage);
				operationCaisse.setIdCaisseDuJour(rs
						.getInt("id_caisse_du_jour"));
				operationCaisse.setProduit(UccViewer.INSTANCE.leProduitNum(rs
						.getInt("id_produit")));
				operationCaisse.setQuantite(rs.getInt("quantite"));
				operationCaisse.setVenteAnimateur(rs
						.getBoolean("vente_animateur"));
			}
			return operationCaisse;
		} catch (SQLException e) {
			System.out.println("[E][SQL] : ");
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<OperationCaisseDTO> lesOperationsCaisseDe(
			CaisseDuJourDTO caisseDuJour) {
		ResultSet rs = null;
		ArrayList<OperationCaisseDTO> listeOperationCaisse = new ArrayList<OperationCaisseDTO>();
		try {
			q2.setInt(1, caisseDuJour.getId());
			rs = q2.executeQuery();
			while (rs.next()) {
				OperationCaisseDTO operationCaisse = BizFactory.INSTANCE
						.createEmptyOperationCaisseDTO();

				operationCaisse.setId(rs.getLong("id_operation_caisse"));
				Calendar horodatage = Calendar.getInstance();
				horodatage.setTimeInMillis(rs.getTimestamp("horodatage")
						.getTime());
				operationCaisse.setHorodatage(horodatage);
				operationCaisse.setIdCaisseDuJour(rs
						.getInt("id_caisse_du_jour"));
				operationCaisse.setProduit(UccViewer.INSTANCE.leProduitNum(rs
						.getInt("id_produit")));
				operationCaisse.setQuantite(rs.getInt("quantite"));
				operationCaisse.setVenteAnimateur(rs
						.getBoolean("vente_animateur"));

				listeOperationCaisse.add(operationCaisse);
			}
			return listeOperationCaisse;
		} catch (SQLException e) {
			System.out.println("[E][SQL] : ");
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<OperationCaisseDTO> lesOperationsCaisseDe(int id) {
		ResultSet rs = null;
		ArrayList<OperationCaisseDTO> listeOperationCaisse = new ArrayList<OperationCaisseDTO>();
		try {
			q2.setInt(1, id);
			rs = q2.executeQuery();
			while (rs.next()) {
				OperationCaisseDTO operationCaisse = BizFactory.INSTANCE
						.createEmptyOperationCaisseDTO();

				operationCaisse.setId(rs.getLong("id_operation_caisse"));
				Calendar horodatage = Calendar.getInstance();
				horodatage.setTimeInMillis(rs.getTimestamp("horodatage")
						.getTime());
				operationCaisse.setHorodatage(horodatage);
				operationCaisse.setIdCaisseDuJour(rs
						.getInt("id_caisse_du_jour"));
				operationCaisse.setProduit(UccViewer.INSTANCE.leProduitNum(rs
						.getInt("id_produit")));
				operationCaisse.setQuantite(rs.getInt("quantite"));
				operationCaisse.setVenteAnimateur(rs
						.getBoolean("vente_animateur"));

				listeOperationCaisse.add(operationCaisse);
			}
			return listeOperationCaisse;
		} catch (SQLException e) {
			System.out.println("[E][SQL] : ");
			e.printStackTrace();
		}
		return null;
	}
}
